package com.ccdt.tv.smartrecommend.utils;

import android.util.Log;

import java.lang.reflect.Method;

/**
 * Created by wudz on 2017/3/13.
 */

public class SystemPropUtil {
    private static final String TAG="SystemPropUtil";
    /**
     * 通过反射方式取值【android.os.SystemProperties】
     *
     * @param property
     * @return
     */
    public static String getPropertiesString(String property, String defaultValue) {
        String ret = defaultValue;
        try {
            Class<?> cls = Class.forName("android.os.SystemProperties");
            Method method = cls.getDeclaredMethod("get", String.class, String.class);
            ret = (String) method.invoke(cls, property, defaultValue);
        } catch (Exception e) {
            Log.e("SystemPropUtil", "getPropertiesString error", e);
        }
        if (isEmpty(ret)) {
            ret = defaultValue;
        }
        return ret;
    }
    public static void setPropertiesString(String property, String value) {
        Log.i(TAG,"setPropertiesString:"+property+" value:"+value);
        String ret = value;
        try {
            Class<?> cls = Class.forName("android.os.SystemProperties");
            Method method = cls.getDeclaredMethod("set", String.class,String.class);
            method.invoke(cls, property,value);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("SystemPropUtil", "setPropertiesString error", e);
        }
    }
    private static boolean isEmpty(String arg) {
        return arg == null || arg.trim().length() <= 0;
    }
}
